﻿using System.Diagnostics.CodeAnalysis;
using KouDai.Core.Service;
using System.IO;
using System;
#if NETFX_CORE
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;
#else
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
#endif

namespace KouDai.Share
{
    public static class LowMemoryImageLoader
    {

        /// <summary>
        /// Gets the value of the Uri to use for providing the contents of the Image's Source property.
        /// </summary>
        /// <param name="obj">Image needing its Source property set.</param>
        /// <returns>Uri to use for providing the contents of the Source property.</returns>
        [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "UriSource is applicable only to Image elements.")]
        public static string GetUriSource(Image obj)
        {
            if (null == obj)
            {
                throw new ArgumentNullException("obj");
            }
            return (string)obj.GetValue(UriSourceProperty);
        }

        /// <summary>
        /// Sets the value of the Uri to use for providing the contents of the Image's Source property.
        /// </summary>
        /// <param name="obj">Image needing its Source property set.</param>
        /// <param name="value">Uri to use for providing the contents of the Source property.</param>
        [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "UriSource is applicable only to Image elements.")]
        public static void SetUriSource(Image obj, string value)
        {
            if (null == obj)
            {
                throw new ArgumentNullException("obj");
            }
            obj.SetValue(UriSourceProperty, value);
        }

        /// <summary>
        /// Identifies the UriSource attached DependencyProperty.
        /// </summary>
        public static readonly DependencyProperty UriSourceProperty = DependencyProperty.RegisterAttached(
            "UriSource", typeof(string), typeof(LowMemoryImageLoader), new PropertyMetadata(null, OnUriSourceChanged));

        private static async void OnUriSourceChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            Image image = (Image)obj;
            string imageUrl = (string)e.NewValue;
            Stream stream = await SuperImageService.Instance.CacheImage(imageUrl);

#if NETFX_CORE
            MemoryStream ms = new MemoryStream();
            stream.CopyTo(ms);
            byte[] bytes = ms.ToArray();

            InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream();
            DataWriter dw = new DataWriter(ras.GetOutputStreamAt(0));
            dw.WriteBytes(bytes);
            await dw.StoreAsync();
            ras.Seek(0);

            BitmapImage bitmapImage = new BitmapImage();
            bitmapImage.SetSource(ras);
# else

            BitmapImage bitmapImage = new BitmapImage();
            bitmapImage.SetSource(stream);
#endif
            image.Source = bitmapImage;
        }

    }
}
